from pymongo import MongoClient
from bson.objectid import ObjectId

class MongoDBHelper():
    def __init__(self,db,ip='',port=60004,uid='',pwd=''):
        self.client = MongoClient(ip,port)
        self.db = self.client['admin']
        self.db.authenticate(uid,pwd)
        self.db = self.client[db]

    def excute(self,tbl,query):
        table = self.db[tbl]
        result = []
        for i in table.find(query):
            result.append(i)
        return result

    def insert(self,tbl,data):
        #避免插入后 原数据被改变 增加了_id
        d = copy.deepcopy(data)
        table = self.db[tbl]
        table.insert(d)

    def remove(self,tbl,data):
        table = self.db[tbl]
        table.remove(data)

    def update(self,tbl,query,update):
        table = self.db[tbl]
        return table.update(query,update)

    def delete(self,tbl):
        self.db.drop_collection(tbl)


